<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="合同名称:" prop="htName">
        <el-input
          v-model="queryParams.htName"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号:" prop="htBh">
        <el-input
          v-model="queryParams.htBh"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程状态:" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择流程状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.process_business_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请变更事项:" prop="sqBgSx">
        <el-select v-model="queryParams.sqBgSx" placeholder="请选择申请变更事项" clearable>
          <el-option
            v-for="dict in dict.type.htbg_sqbg_sx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['htbg:htbg:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['htbg:htbg:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['htbg:htbg:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['htbg:htbg:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['htbg:htbg:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="!selectedRow || (selectedRow.processStatus !== 1 && selectedRow.processStatus !== 4)"
          @click="handleStartTask"
        >发起申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-delete"
          @click="handleRevokeProcess"
          :disabled="!selectedRow || selectedRow.processStatus !== 2"
        >撤销流程</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-delete"
          :disabled="single"
          @click="handleExports"
        >导出docx</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              @row-click="clickRow" ref="table" highlight-current-row v-loading="loading"
              :data="htbgList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column show-overflow-tooltip label="合同名称" align="center" prop="htName" width="300">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.htName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="合同编号" align="center" prop="htBh" width="120"/>
      <el-table-column label="申请日期" align="center" prop="sqTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sqTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="申请单位/项目部" align="center" prop="sqDw" />
      <el-table-column label="申请变更事项" show-overflow-tooltip align="center" prop="sqBgSx" width="200">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.htbg_sqbg_sx" :value="scope.row.sqBgSx"/>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="lxPhone" width="150"/>
      <el-table-column label="状态" align="center" prop="processStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.process_business_status" :value="scope.row.processStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.processStatus==1 || scope.row.processStatus==4"
            v-hasPermi="['htbg:htbg:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.processStatus==1 || scope.row.processStatus==4"
            v-hasPermi="['htbg:htbg:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同变更对话框 -->
    <vxe-modal :title="title" v-model="open" width="80%" showFooter show-zoom resize >
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="140px">
        <el-tabs type="border-card" v-model="activeName">

          <!-- 基本资料 -->
          <el-tab-pane name="jbZl" :lazy=true>
            <span slot="label"><i class="vxe-icon-feedback"></i> 基本资料</span>
            <div v-if="activeName === 'jbZl'" style="height: 75vh; overflow-y: auto;">
              <!-- 变更信息 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">变更信息</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="合同名称:" prop="htName">
                      <el-input disabled v-model="form.htName" placeholder="请输入合同名称"  @focus="selectHt">
                      <el-button slot="append" icon="el-icon-search" @click="selectHt" style="color: #1890ff">查询课题列表
                      </el-button>
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="合同编号:" prop="htBh">
                      <el-input disabled v-model="form.htBh" placeholder="请输入合同编号" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="申请日期:" prop="sqTime">
                      <el-date-picker disabled style="width: 100%;" clearable
                                      v-model="form.sqTime"
                                      type="date"
                                      value-format="yyyy-MM-dd"
                                      placeholder="请选择申请日期">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="申请单位/项目部:" prop="sqDw">
                      <el-input disabled v-model="form.sqDw" placeholder="请输入申请单位/项目部" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="联系电话:" prop="lxPhone">
                      <el-input v-model="form.lxPhone" placeholder="请输入联系电话" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="申请变更事项:" prop="sqBgSx">
                      <el-select :multiple="true" v-model="form.sqBgSx" placeholder="请选择申请变更事项">
                        <el-option
                          v-for="dict in dict.type.htbg_sqbg_sx"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="变更原因:" prop="bgYy">
                  <el-input v-model="form.bgYy" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="变更内容:" prop="bgNr">
                  <el-input v-model="form.bgNr" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="执行事项时间说明:" prop="zxSm">
                  <el-input v-model="form.zxSm" type="textarea" placeholder="请输入内容" />
                </el-form-item>
              </el-card>
              <!-- 项目名称变更 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">项目名称变更</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="原项目名称" prop="yXmName">
                      <el-input disabled v-model="form.yXmName" placeholder="请输入原项目名称" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="变更项目名称" prop="bgXmName">
                      <el-input v-model="form.bgXmName" placeholder="请输入变更项目名称" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
              <!-- 合同延期 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">项目名称变更</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="原起年月" prop="yQTime">
                      <el-date-picker disabled clearable
                                      v-model="form.yQTime"
                                      type="month"
                                      value-format="yyyy-MM"
                                      placeholder="请选择原起年月">
                      </el-date-picker>
                      至
                      <el-date-picker disabled clearable
                                      v-model="form.yZTime"
                                      type="month"
                                      value-format="yyyy-MM"
                                      placeholder="请选择原止年月">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="变更起年月" prop="bgQTime">
                      <el-date-picker clearable
                                      v-model="form.bgQTime"
                                      type="month"
                                      value-format="yyyy-MM"
                                      placeholder="请选择变更起年月">
                      </el-date-picker>
                      至
                      <el-date-picker clearable
                                      v-model="form.bgZNy"
                                      type="month"
                                      value-format="yyyy-MM"
                                      placeholder="请选择变更止年月">
                      </el-date-picker>
                    </el-form-item>

                  </el-col>

                </el-row>
              </el-card>
              <!-- 原负责人情况 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">原负责人情况</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="原负责人姓名" prop="yFzName">
                      <el-input disabled v-model="form.yFzName" placeholder="请输入原负责人姓名" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="原负责人地址" prop="yFzDz">
                      <el-input disabled v-model="form.yFzDz" placeholder="请输入原负责人地址" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="原负责人邮编" prop="yFzYb">
                      <el-input disabled v-model="form.yFzYb" placeholder="请输入原负责人邮编" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="原负责人邮箱" prop="yFzYx">
                      <el-input disabled v-model="form.yFzYx" placeholder="请输入原负责人邮箱" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="原负责人电话" prop="yFzPhone">
                      <el-input disabled v-model="form.yFzPhone" placeholder="请输入原负责人电话" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="原负责人传真" prop="yFzrCz">
                      <el-input disabled v-model="form.yFzrCz" placeholder="请输入原负责人传真" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
              <!-- 变更负责人情况 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">变更负责人情况</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="变更负责人姓名" prop="bgFzName">
                      <el-input v-model="form.bgFzName" placeholder="请输入变更负责人姓名" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="变更负责人地址" prop="bgFzDz">
                      <el-input v-model="form.bgFzDz" placeholder="请输入变更负责人地址" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="变更负责人邮编" prop="bgFzYb">
                      <el-input v-model="form.bgFzYb" placeholder="请输入变更负责人邮编" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="变更负责人邮箱" prop="bgFzYx">
                      <el-input v-model="form.bgFzYx" placeholder="请输入变更负责人邮箱" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="变更负责人电话" prop="bgFzPhone">
                      <el-input v-model="form.bgFzPhone" placeholder="请输入变更负责人电话" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="变更负责人传真" prop="bgFzCz">
                      <el-input v-model="form.bgFzCz" placeholder="请输入变更负责人传真" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
              <!-- 原项目预算经费 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">原项目预算经费</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="原项目总经费(万元)" prop="yXmZjf">
                      <el-input disabled v-model="form.yXmZjf" placeholder="请输入原项目总经费(万元)" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="原甲方支付(万元)" prop="yJfZf">
                      <el-input disabled v-model="form.yJfZf" placeholder="请输入原甲方支付(万元)" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="原乙方自筹(万元)" prop="yYfZc">
                      <el-input v-model="form.yYfZc" placeholder="请输入原乙方自筹(万元)" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
              <!-- 变更项目预算经费 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">变更项目预算经费</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <el-form-item label="变更项目总经费(万元)" prop="bgXmZjf">
                      <el-input v-model="form.bgXmZjf" placeholder="请输入变更项目总经费(万元)" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="变更甲方支付(万元)" prop="bgJfZf">
                      <el-input v-model="form.bgJfZf" placeholder="请输入变更甲方支付(万元)" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="变更乙方自筹(万元)" prop="bgYfZc">
                      <el-input v-model="form.bgYfZc" placeholder="请输入变更乙方自筹(万元)" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
              <!-- 原项目经费支付计划 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">原项目经费支付计划</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="原合同总额(万元)" prop="yHtZe">
                      <el-input disabled v-model="form.yHtZe" placeholder="请输入原合同总额(万元)" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="原甲方向乙方提供经费(万元)" prop="yJfToYf">
                      <el-input disabled v-model="form.yJfToYf" placeholder="请输入原甲方向乙方提供经费(万元)" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
              <!-- 变更项目经费支付计划 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">变更项目经费支付计划</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="变更合同总额(万元)" prop="bgHtZe">
                      <el-input v-model="form.bgHtZe" placeholder="请输入变更合同总额(万元)" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="变更甲方向乙方提供经费(万元)" prop="bgJfToYf">
                      <el-input v-model="form.bgJfToYf" placeholder="请输入变更甲方向乙方提供经费(万元)" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
              <!-- 变更课题组成人员情况 -->
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">变更课题组成人员情况</span>
                </div>
                <el-divider content-position="center">变更课题组成人员情况信息</el-divider>
                <el-row :gutter="10" class="mb8">
                  <el-col :span="1.5">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddHtglHtbgZcry">添加</el-button>
                  </el-col>
                  <el-col :span="1.5">
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHtglHtbgZcry">删除</el-button>
                  </el-col>
                </el-row>
                <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                          :data="htglHtbgZcryList" :row-class-name="rowHtglHtbgZcryIndex"
                          @selection-change="handleHtglHtbgZcrySelectionChange" ref="htglHtbgZcry">
                  <el-table-column type="selection" width="50" align="center" />
                  <el-table-column label="序号" align="center" prop="index" width="50"/>
                  <el-table-column align="center" label="姓名" prop="name" width="100"/>
                  <el-table-column align="center" label="年龄" prop="age" width="70"/>
                  <el-table-column align="center" label="职务" prop="zhiWu" width="150"/>
                  <el-table-column align="center" label="职称" prop="zhiChen" width="150"/>
                  <el-table-column align="center" show-overflow-tooltip label="专业" prop="zy" width="150"/>
                  <el-table-column align="center" show-overflow-tooltip label="本课题中分工" prop="bKtFg">
                    <template slot-scope="scope">
                      <el-input :title="scope.row.bKtFg" v-model="scope.row.bKtFg" placeholder="请输入本课题中分工" />
                    </template>
                  </el-table-column>
                  <el-table-column align="center" label="投入课题工作总月数" prop="trZys" width="150">
                    <template slot-scope="scope">
                      <el-input :title="scope.row.trZys" v-model="scope.row.trZys" placeholder="请输入投入课题工作总月数" />
                    </template>
                  </el-table-column>
                  <el-table-column align="center" show-overflow-tooltip label="工作单位" prop="gzDw"/>
                  <el-table-column v-if="1 === 2" label="科技人员ID" prop="kjryId" width="150"/>
                  <el-table-column align="center" label="操作" width="120">
                    <template slot-scope="scope">
                      <el-button type="text" icon="el-icon-arrow-up" size="mini" @click="moveUp(scope.$index)" :disabled="scope.$index === 0">上移</el-button>
                      <el-button type="text" icon="el-icon-arrow-down" size="mini" @click="moveDown(scope.$index)" :disabled="scope.$index === htglHtbgZcryList.length - 1">下移</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-card>

            </div>
          </el-tab-pane>
          <!-- 附件资料 -->
          <el-tab-pane v-if="this.form.idWhere != null" name="fjZl" :lazy=true>
            <span slot="label"><i class="vxe-icon-paste"></i> 附件资料</span>
            <div v-if="activeName === 'fjZl'" style="height: 75vh; overflow-y: auto;">
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <!-- 上传按钮 -->
                  <el-button size="mini" icon="el-icon-upload" type="primary" @click="htBgFjUpload">上传</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHtglHtbgFj">删除</el-button>
                </el-col>
              </el-row>
              <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                        :data="htglHtbgFjList" @selection-change="handleHtglHtbgFjSelectionChange" ref="htglHtbgFj">
                <el-table-column type="selection" width="50" align="center" />
                <el-table-column label="序号" align="center" type="index" width="50"/>
                <el-table-column label="文件名称" prop="fileName" :formatter="getFileNames"/>
                <el-table-column label="文件大小" prop="fileSize" width="200"/>
                <el-table-column label="操作" align="center">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleDownload(scope.row)"
                    >下载
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-tab-pane>

        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>

    <!-- 科技人员附件上传弹窗 -->
    <vxe-modal title="附件上传" v-model="fjUpload" width="25%" showFooter show-zoom resize @close="fjClose">
      <el-upload
        align="center"
        class="upload-demo"
        multiple
        :action="uploadFileUrl"
        :before-upload="handleBeforeUpload"
        :file-list="fileList"
        :limit="20"
        :on-error="handleUploadError"
        :on-exceed="handleExceed"
        :on-success="handleUploadSuccess"
        :headers="headers"
        ref="fileUpload"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          请上传
          <template> 大小不超过 <b style="color: #f56c6c">{{ 50 }}MB</b></template>
          <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b></template>
          的文件
        </div>
      </el-upload>

      <div slot="footer" class="dialog-footer" style="display: flex; justify-content: center;">
        <el-button v-if="allFilesUploaded === false" size="mini" type="primary" @click="fjUpload = false">关 闭
        </el-button>
        <el-button v-if="allFilesUploaded === true" size="mini" type="primary" @click="fjClose">上传完成, 关闭
        </el-button>
      </div>

    </vxe-modal>

    <!-- 导入组件 -->
    <HtBgDr ref="htBgDr"/>

    <!-- 合同单选组件 -->
    <HtDx ref="htDx"/>

    <!-- 查询进度 -->
    <Jindu ref="picture"/>

    <!-- 合同变更详情组件 -->
    <HtBgXq ref="htBgXq"/>
  </div>
</template>

<script>
import {
  addHtbg,
  delHtbg, delHtBgFj, exportHtBgWord,
  getHtbg,
  getHtBgId,
  insertHtbgFjByHtbgId,
  listHtbg,
  selectHtbgFjList, selectHtglHtbgZcryByHtbgId,
  updateHtbg
} from "@/api/xmgl/htgl/htbg/htbg";
import {getToken} from "@/utils/auth";
import HtBgDr from "@/views/xmgl/htgl/htbg/components/HtBgDr"
import {getUser} from "@/api/system/user";
import * as huacai from "@/utils/huacai";
import HtDx from "@/views/components/htXz/htDx.vue";
import {selectHtglHtbzCdRyByHtbzId} from "@/api/xmgl/htgl/htbz/htbz";
import Jindu from "@/views/components/liuChengState/jindu";
import {revokeProcess, startTask} from "@/api/activiti/task";
import HtBgXq from "@/views/xmgl/htgl/htbg/components/HtBgXq";
import {delFj} from "@/api/xmgl/lxgl/lxwj/lxwj";

export default {
  name: "Htbg",
  components: {HtBgXq, Jindu, HtDx, HtBgDr},
  dicts: ['htbg_sqbg_sx', 'rcpt_zc', 'process_business_status'],
  provide() {
    return {
      updateHtLabel: (val) => {
        this.form.htName = val.htName //合同名称
        this.form.htBh = val.htBh //合同编号
        // 获取年月日
        let year = new Date().getFullYear();
        let month = (new Date().getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的，所以要加1，并确保两位数
        let day = new Date().getDate().toString().padStart(2, '0'); // 确保两位数
        // 格式化为 "年-月-日"
        this.form.sqTime = year + '-' + month + '-' + day; //申请日期
        this.form.sqDw = this.deptLable //申请单位/项目部
        this.form.yXmName = val.htName //原项目名称
        this.form.yQTime = val.qTime //原起年月
        this.form.yZTime = val.zTime //原止年月
        this.form.yFzName = val.fzName //原负责人姓名
        this.form.yFzDz = val.fzDz //原负责人地址
        this.form.yFzYb = val.fzYb //原负责人邮编
        this.form.yFzYx = val.fzYx //原负责人邮箱
        this.form.yFzPhone = val.fzPhone //原负责人电话
        this.form.yFzrCz = val.fzCz //原负责人传真
        this.form.yXmZjf = val.xmZjf //原项目总经费
        this.form.yJfZf = val.jfZf //原甲方支付
        this.form.yYfZc = val.yfZc //原乙方自筹
        this.form.yHtZe = val.htZe //原合同总额
        this.form.yJfToYf = val.jfToYf //原甲方向乙方提供经费
        selectHtglHtbzCdRyByHtbzId(val.htbzId).then(res => {
          this.htglHtbgZcryList = res.data.htglHtbzCdryList
        })
      }
    };
  },
  data() {
    return {
      selectedRow: null, // 保存当前选中的行
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      fjUpload: false, //附件上传弹窗
      //要修改的 合同变更id
      htbgIdEdit: "",
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
      getHtBgId: null, //从后端获取到的合同变更主键ID
      activeName: 'jbZl', //默认打开的标签
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedHtglHtbgFj: [],
      // 组成人员子表选中数据
      checkedHtglHtbgZcry: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同变更表格数据
      htbgList: [],
      // 合同变更附件表格数据
      htglHtbgFjList: [],
      // 变更课题组成人员情况表格数据
      htglHtbgZcryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        idWhere: null,
        htName: null,
        htBh: null,
        sqTime: null,
        sqDw: null,
        lxPhone: null,
        sqBgSx: null,
        userId: this.$store.state.user.id,
        deptId: null,
        processStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        htName: [{ required: true, message: "合同名称不能为空", trigger: "blur" }],
        htBh: [{ required: true, message: "合同编号不能为空", trigger: "blur" }],
        sqTime: [{ required: true, message: "申请日期不能为空", trigger: "blur" }],
        sqDw: [{ required: true, message: "申请单位/项目部不能为空", trigger: "blur" }],
        lxPhone: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
        sqBgSx: [{ required: true, message: "申请变更事项不能为空", trigger: "change" }],
        bgYy: [{ required: true, message: "变更原因不能为空", trigger: "blur" }],
        bgNr: [{ required: true, message: "变更内容不能为空", trigger: "blur" }],
        zxSm: [{ required: true, message: "执行事项时间说明不能为空", trigger: "blur" }],
      },
      deptLable: null, //当前登录用户的详细部门名称
    };
  },
  created() {
    this.getList();
    this.getDeptName()
  },
  methods: {
    //导出按钮操作
    handleExports() {
      const row = this.selectedRow; // 获取当前选中的行
      const queryParams = this.queryParams;
      const htbgId = row.htbgId; // Assuming htbgId is defined somewhere in your component
      this.$confirm('是否确认导出word文档?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true
        return exportHtBgWord(queryParams, htbgId); // Passing htbgId to the exportWord function
      }).then(response => {
        console.log('/profile/upload/' + response.msg)
        this.$download.resource('/profile/upload/' + response.msg);
        this.loading = false
      }).catch(() => {});
    },
    // 查询详情信息
    handleButtonClick(row) {
      this.$refs.htBgXq.handleOpen(row);
    },
    //发起任务
    handleStartTask() {
      const row = this.selectedRow; // 获取当前选中的行
      if (!row) {
        this.$message.error("请先选择一行数据");
        return;
      }
      this.$confirm("是否确认发起申请？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          startTask({ businessKey: row.htbgId, businessType: 5 }).then(res => {
            this.getList();
            this.$modal.msgSuccess("发起成功");
            this.selectedRow = null
          });
        })
        .catch(() => {});
    },
    //撤销流程
    handleRevokeProcess() {
      const row = this.selectedRow; // 获取当前选中的行
      if (!row) {
        this.$message.error("请先选择一行数据");
        return;
      }
      this.$confirm("是否确撤销申请？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {
            businessKey: row.htbgId,
            businessType: 5,
            processInstanceId: row.processInstanceId
          };
          revokeProcess(params).then(res => {
            this.getList();
            this.$modal.msgSuccess("撤销成功");
            this.selectedRow = null
          });
        })
        .catch(() => {});
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    // 组成人员上移方法
    moveUp(index) {
      if (index > 0) {
        const item = this.htglHtbgZcryList.splice(index, 1)[0];
        this.htglHtbgZcryList.splice(index - 1, 0, item);
      }
    },
    // 组成人员下移方法
    moveDown(index) {
      if (index < this.htglHtbgZcryList.length - 1) {
        const item = this.htglHtbgZcryList.splice(index, 1)[0];
        this.htglHtbgZcryList.splice(index + 1, 0, item);
      }
    },
    // 选择关联合同
    selectHt() {
      this.$refs.htDx.handleOpen();
    },
    // 获取当前登录用户的部门详细名称(包含上级名称)
    getDeptName() {
      let getUserId = this.$store.state.user.id //当前登录用户的用户ID
      getUser(getUserId).then(res => {
        huacai.getFullDeptLabel(res.data.dept.deptId).then(fullLabel => {
          this.deptLable = fullLabel; // 获取当前登录用户的部门详细名称(包含上级名称)
        });
      })
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

        // 取消之前选中的行，并选中当前点击的行
        selectedRows.forEach(selectedRow => {
          table.toggleRowSelection(selectedRow);
        });
        table.toggleRowSelection(row);
    },
    // 文件下载处理
    handleDownload(row) {
      const resource = row.fileName;
      // 默认方法
      this.$download.resource(resource);
    },
    // 获取文件名原名
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    //关闭上传附件弹窗后
    fjClose() {
      this.fileList = []
      this.fjUpload = false
      this.allFilesUploaded = false
    },
    //上传文件之前的钩子
    handleBeforeUpload(file){
      let that = this;
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
          return false;
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 / 1024 < this.fileSize;
          if (!isLt) {
            this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
            return false;
          }
        }
        this.$modal.loading("正在上传文件，请稍候...");
        this.number++;
        return true;
      }
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        strs += list[i].url + separator;
      }
      return strs !== '' ? strs.substr(0, strs.length - 1) : '';
    },
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传成功
    handleUploadSuccess(res, file) {
      let that = this;
      if (res.code === 200) {
        this.uploadList.push({name: res.fileName, url: res.fileName, size: that.formatFileSize(file.size)});
        let xzItemFile = { //新增
          "htbgId": that.getHtBgId,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        let xgItemFile = { //修改
          "htbgId": that.htbgIdEdit,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        if (this.xg === false) {
          // 点击新增按钮的上传
          insertHtbgFjByHtbgId(xzItemFile).then(resFil => {
            that.getHtBgFjList();
          })
        } else if (this.xg === true){
          // 点击修改按钮的上传
          insertHtbgFjByHtbgId(xgItemFile).then(resFil => {
            that.getHtBgFjList();
          })
        }
        this.uploadedSuccessfully();
      } else {
        this.number--;
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.fileUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
      this.allFilesUploaded = true
      this.$modal.msgSuccess("上传成功")
      // this.fjShangChuan = false
    },
    getHtBgFjList() {
      let that = this;
      if (this.xg === false) {
        //点击新增 上传文件后的文件列表
        selectHtbgFjList(that.getHtBgId).then(resFileList => {
          that.htglHtbgFjList = resFileList.data;
        })
      } else {
        //点击修改 上传文件后的文件列表
        selectHtbgFjList(that.htbgIdEdit).then(resFileList => {
          that.htglHtbgFjList = resFileList.data;
        })
      }
    },
    // 显示文件大小为KB, MB, GB, TB
    formatFileSize(size) {
      if (size === 0) return '0 Bytes';
      const k = 1024;
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      const i = Math.floor(Math.log(size) / Math.log(k));
      return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    },
    // 获取文件名称
    getFileName(name) {
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.number > 0 && this.uploadList.length === this.number) {
        this.fileList = this.fileList.concat(this.uploadList);
        this.fileList = this.fileList.map(item => {
          // 使用replace方法去除name中的特定前缀
          item.name = item.name.split('/').pop();
          return item;
        });
        this.uploadList = [];
        this.number = 0;
        this.$emit("input", this.listToString(this.fileList));
        this.$modal.closeLoading();
      }
    },
    //附件上传按钮
    htBgFjUpload() {
      this.fjUpload = true;
    },
    /** 查询合同变更列表 */
    getList() {
      this.loading = true;
      listHtbg(this.queryParams).then(response => {
        this.htbgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        htbgId: this.getHtBgId, idWhere: null, htName: null, htBh: null, sqTime: null, sqDw: this.deptLable, lxPhone: null, sqBgSx: null,
        bgYy: null, bgNr: null, zxSm: null, yXmName: null, bgXmName: null, yQTime: null, yZTime: null, bgQTime: null,
        bgZNy: null, yFzName: null, yFzDz: null, yFzYb: null, yFzYx: null, yFzPhone: null, yFzrCz: null, bgFzName: null,
        bgFzDz: null, bgFzYb: null, bgFzYx: null, bgFzPhone: null, bgFzCz: null, yXmZjf: null, yJfZf: null, yYfZc: null,
        bgXmZjf: null, bgJfZf: null, bgYfZc: null, yHtZe: null, yJfToYf: null, bgHtZe: null, bgJfToYf: null, createBy: null,
        createTime: null, updateBy: null, updateTime: null, userId: null, deptId: null
      };
      this.htglHtbgFjList = [];
      this.htglHtbgZcryList = [];
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.htbgId)
      if (ids.length === 1) {
        this.ids = ids[0]
      }else {
        this.ids = ids
      }
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getHtBgId().then(res => { //从后端获取合同变更主键ID (UUID)
        this.getHtBgId = res.msg
        this.reset();
        this.form.sqBgSx = []
      })
      this.selectHt(); //打开合同选择弹窗组件
      this.activeName = 'jbZl'
      this.open = true;
      this.title = "添加合同变更";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      this.activeName = 'jbZl'
      this.xg = true
      const htbgId = row.htbgId || this.ids
      this.htbgIdEdit = row.htbgId || this.ids
      getHtbg(htbgId).then(response => {
        this.form = response.data;
        //修改的时候再变成数组
        if (this.form.sqBgSx) {
          this.form.sqBgSx = this.form.sqBgSx.split(",");
        }
        this.htglHtbgFjList = response.data.htglHtbgFjList;
        this.open = true;
        this.title = "修改合同变更";

        selectHtglHtbgZcryByHtbgId(htbgId).then(res => {
          this.htglHtbgZcryList = res.data.htglHtbgZcryList;
        });

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          this.form.htglHtbgFjList = this.htglHtbgFjList;
          this.form.htglHtbgZcryList = this.htglHtbgZcryList;
          if (this.form.sqBgSx) {
            this.form.sqBgSx = this.form.sqBgSx.join(",")
          }
          if (this.form.idWhere != null) { //修改
            updateHtbg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.xg = false
            });
          } else { //新增
            addHtbg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const htbgIds = row.htbgId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function() {
        return delHtbg(htbgIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 合同变更附件删除按钮操作 */
    handleDeleteHtglHtbgFj() {
      if (this.checkedHtglHtbgFj.length == 0) {
        this.$modal.msgError("请先选择要删除的合同变更附件数据");
      } else {
        const fjIds = this.checkedHtglHtbgFj;
        this.$modal.confirm('确定删除选中记录？').then(function() {
          return delHtBgFj(fjIds);
        }).then(() => {
          getHtbg(this.htbgIdEdit).then(response => {
            this.htglHtbgFjList = response.data.htglHtbgFjList;
          });
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
    /** 复选框选中数据 */
    handleHtglHtbgFjSelectionChange(selection) {
      this.checkedHtglHtbgFj = selection.map(item => item.fjId)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('htbg/htbg/export', {
        ...this.queryParams
      }, `htbg_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.htBgDr.handleImport();
    },
    /** 变更课题组成人员情况序号 */
    rowHtglHtbgZcryIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 变更课题组成人员情况添加按钮操作 */
    handleAddHtglHtbgZcry() {
      let obj = {};
      obj.name = "";
      obj.age = "";
      obj.zhiWu = "";
      obj.zhiChen = "";
      obj.zy = "";
      obj.bKtFg = "";
      obj.trZys = "";
      obj.gzDw = "";
      obj.kjryId = "";
      this.htglHtbgZcryList.push(obj);
    },
    /** 变更课题组成人员情况删除按钮操作 */
    handleDeleteHtglHtbgZcry() {
      if (this.checkedHtglHtbgZcry.length == 0) {
        this.$modal.msgError("请先选择要删除的变更课题组成人员情况数据");
      } else {
        const htglHtbgZcryList = this.htglHtbgZcryList;
        const checkedHtglHtbgZcry = this.checkedHtglHtbgZcry;
        this.htglHtbgZcryList = htglHtbgZcryList.filter(function(item) {
          return checkedHtglHtbgZcry.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleHtglHtbgZcrySelectionChange(selection) {
      this.checkedHtglHtbgZcry = selection.map(item => item.index)
    },

  }
};
</script>
